import axios from 'axios';
import {BASE_URL} from "@/common/const";
import { useUserStore } from '@/store/modules/user'
import JSONBIG from 'json-bigint';
import { ElMessage, ElMessageBox } from 'element-plus'

//修复超过16位数字精度丢失问题
axios.defaults.transformResponse = [
  function (data) {
    const json = JSONBIG({
      storeAsString: true
    })
    const res = json.parse(data)
    console.log('wyq', 'json bigint')
    return res
  }
]

const service = axios.create({
  baseURL: BASE_URL, // url = base url + request url
  timeout: 5000, // request timeout
});



service.interceptors.request.use(
    config => {
      const userStore = useUserStore()
      config.headers = {
        ...config.headers,
      }

      config.params = {
        ...config.params,
        'cv': `ZCAMP${__APP_VERSION__}_Web`,
        'sid': config.params&&config.params.sid ? config.params.sid : userStore.sid,
        'uid': config.params&&config.params.uid ? config.params.uid : userStore.uid,
      };

      return config;
    },
    error => {
      console.log('[request拦截]接口报错:' + error); // for debug
      return Promise.reject(error);
    },
);

service.interceptors.response.use(
    response => {
      if (response.data.code === 0 || response.headers.success === 'true') {
        return response.data;
      } else {
        return response.data;
      }
    },
    error => {
      console.log('[response拦截]接口报错:' + error);
      return Promise.reject(error);
    },
);

export default service;
